<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="wangyuguo">
<title>新建司机帐户</title>
<script language="javascript" src="../script/common.js"
	type="text/javascript"></script>

<!--动态js-->
<script type="text/javascript">
	var companyUserId;				//车队id
	var driverId;					//司机id
	var formName = "cardUserEditForm";						//编辑帐户的form name
	var resultData;						//
	var isNewDriver = false;
	var anonymous = true;
	var defDept;
	var tabs = [
		{name:"anodriver",text:"匿名帐户",tabFunc:addAnoDri},
		{name:"perdriver",text:"有档案帐户",tabFunc:addDriver}
	]
	function load(){
		setPages(["userview","anonuserview"]);//切换页面
		Common.initDict(customerform.idnoType,"idno_type","请选择");
		Common.initMultiSelect("merchandiseLimitTD","可购商品","userform","merLimitSel","/card/sel_goods_category.html",460,15);
		Common.initMultiSelect("oilLimitTD","可购油品","userform","oilLimitSel","/card/sel_goodsgroup.html",460,256);
		Common.initMultiSelect("stationLimitTD","定点油站","userform","stationLimitSel","/card/sel_station_limit.html",500,10);
		
		driverId = getPara("cardUserId");							//非空，为编辑司机
		companyUserId = getPara("companyUserId");
		
		if (!companyUserId){
			showWarning("没有指定单位！");
			return;
		}
		var isView = getPara("isView");
		if (isView){
			CardMgnt.showViewForm("customerform");
			CardMgnt.showViewForm("userform");
			if(typeof parent.getDlgBtnDiv == "function"){
				var div = parent.getDlgBtnDiv()
				div.innerHTML = "<button onclick='dialogBackValue()'>返 回</button>"
			}
		}
		var deptId = getPara("deptId");
		if (deptId){
			DWR.call("mgntQueryService.getCompanyDept",deptId,function(result){
				defDept = result;
				loadCardUser();
			});
		}else
			loadCardUser();
	}	
	function loadCardUser(){
		resultData = parent.pageType == "dialog" ? Common._getOpener().resultData : parent.resultData;
		if (!resultData)
			resultData = DWR.syncCall("customerService.getCustomerAndUser",companyUserId);
		var cardUser = resultData.cardUser;
		var c;
		if (cardUser.type != CardUserType.COMPANY){
			showWarning("不是单位的帐户类型！");return;
		}
		
		Common.showDetail("detview",CardMgnt.parseCardUser(resultData));
		
		if (driverId){
			var res = DWR.syncCall("customerService.getCardUserInfo",driverId);
			var driver = CardMgnt.parseCardUser(res);
			var customer = res.customer;
			if (customer){
				addDriver();
				Common.setValues(customerform,customer);
				customerform.idnoType.disabled = true;
				customerform.idnoType.className = "readonly";
				customerform.idno.disabled = true;
				customerform.idno.className = "readonly";
				$("findCustomerByIdno").style.display = "none";
			}else{
				addAnoDri();
			}
			CardMgnt.cardUser.setValues("userform",driver);
		}else{
			Eui.buildTabs("tabsdiv",tabs);
			$("findCustomerByIdno").style.display = "";
			isNewDriver = true;
			$("anodriver").onclick();
		}
	}
	//新增司机，有档案
	function addDriver(){
		Eui.switchTab($("perdriver"));
		clickTabFunc();
		anonymous = false;
		parent.dialog.addBtn("");
		CardMgnt.activForm("customerform");
		$("customerview").style.display = "";
	}
	function addAnoDri(){
		Eui.switchTab($("anodriver"));
		clickTabFunc();
		anonymous = true;
		if (driverId)
			parent.dialog.addBtn("");
		else
			parent.dialog.addBtn("<label>新增匿名司机数量</label>&nbsp;<input type=text id=anoUserAmount value=1 maxlength=3>");
		$("customerview").style.display = "none";
	}
	
	function clickTabFunc(){
		CardMgnt.resetForm(customerform)
		CardMgnt.resetForm(userform)
		if (isNewDriver){
			var companyUser = resultData.cardUser;
			CardMgnt.cardUser.setValues("userform",{
				deptName : defDept?defDept.deptName:'',
				dept:defDept?defDept.id:null,
				status : CardUserStatus.UNACTIVATED,
				isMerLimit:companyUser.isMerLimit,
				isOilLimit:companyUser.isOilLimit,
				oilLimit : companyUser.oilLimit,
				merchandiseLimit : companyUser.merchandiseLimit,
				stationLimit : defDept&&defDept.stationLimit?defDept.stationLimit:companyUser.stationLimit,
				timeLimit : companyUser.timeLimit,
				quantityLimit : companyUser.quantityLimit,
				amountLimit : companyUser.amountLimit,
				frequency : companyUser.frequency,
				quantityNumber : companyUser.quantityNumber,
				invoiceType : companyUser.invoiceType,
				usePin:companyUser.usePin
			});
		}
	}
	//确定按钮
	function onSave(){
		if (!checkForm(userform)) return;
		var cardUser = CardMgnt.getCardUser("userform");
		cardUser.type = CardUserType.DRIVER;
		cardUser.orgUser = companyUserId;
		if (anonymous){
			if (driverId){
				DWR.call("customerService.savePersonalCardUser",cardUser,function(res){
					dialogBackValue(true);
				},{disCtrl : parent.getSaveBtn()});
			}else{
				var count = parent.$("anoUserAmount").value;
				if (isNaN(count) || count<1) {showWarning("请正确输入增加司机的数量");return;}
				
				DWR.call("customerService.batchSaveAnonCardUser",cardUser,count,function(result){
					dialogBackValue(true);
				},{disCtrl : parent.getSaveBtn()});
			}
		}else{
			if (!checkForm(customerform)) return;
			var customer = Common.getValues(customerform);
			if (!driverId && !customer.checkIdno){
				var c = DWR.syncCall("customerService.getCustomerForCardUser",customer.idno,customer.idnoType);
				if (c){
					if (!showQuestion("档案已经存在，是否在该档案下继续创建客户资料？")){dialogBackValue();return;}
					customer.id = c.id
				}
			}
			DWR.call("customerService.savePersonalCustomerAndUser",customer,cardUser,function(result){
				dialogBackValue(result.cardUser);
			},{disCtrl : parent.getSaveBtn()});
		}
	}
	//选择部门
	function selDept(){
		CardMgnt.selDept(function(dept){
			var form = $("userform");
			form.deptName.value = dept.name;
			form.dept.value = dept.id;
			if(dept.stationLimit){
				CardMgnt.clearLimitSelect($("userform").stationLimitSel,true);
				CardMgnt.setStationLimitCtrl($("userform").stationLimitSel,dept.stationLimit);
			}
		},{orgId:companyUserId})
	}
	
	//选择车辆型号
	function selCarModel(){
		CardMgnt.selCarModel(function(carModel){
			var form = $("userform");
			form.carModelName.value = carModel.name;
			form.carModel.value = carModel.id;
		},{orgId:companyUserId})
	}
	
	function checkCustomerIdno(){
		var idno = customerform.idno.value;
		var idnoType = customerform.idnoType.value;
		if (!idno || !idnoType) {showWarning("证件类型和证件号不能为空！");return};
		var c = DWR.syncCall("customerService.getCustomerForCardUser",idno,idnoType);
		if (!c) c = {}
		c.checkIdno = true;
		if (c.id){
			CardMgnt.customer.setValues(customerform,c);
			CardMgnt.showViewForm(customerform);
			if (!showQuestion("档案已经存在，是否在该档案下继续创建司机资料？")){
				dialogBackValue();
			}
		}else
			showInformation("不存在证件号为"+idno+"的档案！")
	}
</script>
</head>

<body onload="load()">

<div id="detview" class="form">
<div class="formTitle">单位帐号信息</div>
<table cellSpacing="0" cellPadding="4" width="100%" border="0"
	class="viewTable col3">
	<tr>
		<td width="50%"><label>名&nbsp;&nbsp;&nbsp;&nbsp;称</label> <span
			id="name"></span></td>
		<td><label>证件类型</label> <span id="idnoTypeText"></span></td>
		<td><label>证&nbsp;件&nbsp;号</label> <span id="idno"></span></td>
	</tr>
	<tr>
		<td width="50%"><label>客户编号</label> <span id="uniqueId"></span></td>
		<td><label>开户机构</label> <span id="buildFileStation"></span></td>
		<td><label>状&nbsp;&nbsp;&nbsp;&nbsp;态</label> <span
			id="statusText"></span></td>
	</tr>
</table>
</div>

<div id='tabsdiv' align="left" class="width97"></div>
<div class="width97">
<div class="form">
<div id="customerview" style="display: none;">
<form id="customerform" name="customerform"><input type="hidden"
	name="id"><input type="hidden" name="checkIdno">
<div class="formTitle">司机信息</div>
<table cellSpacing="0" cellPadding="4" width="100%" border="0"
	class="col2 label6">
	<tr>
		<td><label>证件类型</label> <select name="idnoType"
			onblur="checkV(this,'',true)"
			onchange="checkV($('customerform').idno,this.value,true)">
			<option></option>
		</select><span class="des">*</span></td>
		<td><label>证&nbsp;件&nbsp;号</label> <input type="text" name="idno"
			onblur="checkV(this,$('customerform').idnoType.value,true)"
			maxlength="20"><span class="des">*</span>
			<a href=# onclick="checkCustomerIdno();" id="findCustomerByIdno" style="display:none;">查看档案是否存在</a>
			</td>
	</tr>
	<tr>
		<td><label>客户名称</label> <input type="text" name="name"
			maxlength="10" onblur="checkV(this,'',true)"><span
			class="des">*</span></td>
		<td><label>电子邮箱</label> <input type="text" name="email"
			onblur="checkV(this,'email',false)"></td>
	</tr>
</table>
</form>
</div>
<form id="userform" name="userform"><input type="hidden"
	name="id"> <input type="hidden" name="dept"> <input
	type="hidden" name="type"> <input type="hidden" name="status">

<table cellSpacing=0 cellPadding=4 width="100%" border=0
	class="col2 label6">
	<tr>
		<td><label>车辆型号</label> <input type="text" name="carModelName"
			readonly class="readonly" onblur="checkV(this,'',false)"
			maxlength="12"><a class="orga" href="#"
			onclick="selCarModel()">&nbsp;&nbsp;</a> <input type="hidden"
			name="carModel"></td>
		<td><label>可加油车牌号</label> <input type="text" name="carLicense"
			onblur="checkV(this,'',false)" maxlength="16"></td>
	</tr>
	<tr>
		<td><label>卡 密 码</label> &nbsp;&nbsp;<input type="radio"
			name="usePin" value="true">启用 &nbsp;&nbsp;<input type="radio"
			name="usePin" value="false" checked>停用</td>
		<td><label>加油频率限制</label> <input type="text" name="frequency"
			onblur="checkV(this,'positiveInt',false)" maxlength="2">(多少天加一次油)</td>
	</tr>
	<tr>
		<td><label>每天加油次数</label> <input type="text" name="timeLimit"
			onblur="checkV(this,'positiveInt',false)" maxlength="2"></td>
		<td><label>每天消费金额</label> <input type="text" name="amountLimit"
			onblur="checkV(this,'positiveAmount',false)" maxlength="16"></td>
	</tr>
	<tr>
		<td><label>每天加油限量</label> <input type="text" name="quantityLimit"
			onblur="checkV(this,'positiveInt',false)" maxlength="4"> 升</td>
		<td><label>每次加油限量</label> <input type="text"
			name="quantityNumber" onblur="checkV(this,'positiveInt',false)"
			maxlength="3"> 升</td>
	</tr>
	<tr>
		<td><label>限制商品</label> &nbsp;&nbsp;<input type="radio"
			name="isMerLimit" value="true"
			onclick="CardMgnt.checkLimitCtrl(userform.merLimitSel,true)">&nbsp;是&nbsp;
		&nbsp;&nbsp;<input type="radio" name="isMerLimit" value="false"
			checked onclick="CardMgnt.checkLimitCtrl(userform.merLimitSel,false)">&nbsp;否
		</td>
		<td><label>限制油品</label> &nbsp;&nbsp;<input type="radio"
			name="isOilLimit" value="true"
			onclick="CardMgnt.checkLimitCtrl(userform.oilLimitSel,true)">&nbsp;是&nbsp;
		&nbsp;&nbsp;<input type="radio" name="isOilLimit" value="false"
			checked onclick="CardMgnt.checkLimitCtrl(userform.oilLimitSel,false)">&nbsp;否
		</td>
	</tr>
	<tr>
		<td id="merchandiseLimitTD"></td>
		<td id="oilLimitTD"></td>
	</tr>
	<tr>
		<td><label>所属部门</label> <input type='text' name='deptName'
			readonly class="readonly"> <a class=orga href=#
			onclick="selDept()">&nbsp;&nbsp;</a></td>
		<td><label>备&nbsp;&nbsp;&nbsp;&nbsp;注</label> <input type="text"
			name="notes" maxlength="20"></td>
	</tr>
	<!tr>
		<td id="stationLimitTD"></td>
		<td>&nbsp;</td>
	</tr>
</table>
</form>
</div>
</div>
</body>
</html>
